package com.example.wangning.mycarofcountry.recommend.vehicle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.mycarofcountry.entity.dao.Recommend_Vehiclelist;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/13.
 */
public class VehicleAdapter extends BaseAdapter {
    private List<Recommend_Vehiclelist> list;
    private VolleySingleton queue;
    private Context context;
    private ImageLoader imageLoader;

    public VehicleAdapter(Context context) {
        this.context = context;
        queue = VolleySingleton.getVolleyIntance();
        list = new ArrayList<>();

        imageLoader = queue.getImageLoader();
    }
    public void addData(List<Recommend_Vehiclelist> data){
        list = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VehicleViewHolder vehicleViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_vehicle_item,parent,false);
            vehicleViewHolder = new VehicleViewHolder(convertView);
            convertView.setTag(vehicleViewHolder);
        }else{
            vehicleViewHolder = (VehicleViewHolder) convertView.getTag();
        }
        if (list != null && list.size() > 0){
            Recommend_Vehiclelist vehiclelist = list.get(position);
            if (vehiclelist != null){
                vehicleViewHolder.titleTv.setText(vehiclelist.getTitle());
                vehicleViewHolder.timeTv.setText(vehiclelist.getAdvancetime());
                vehicleViewHolder.reviewcountTv.setText(String.valueOf(vehiclelist.getReviewcount()));
                vehicleViewHolder.typenameTv.setText(vehiclelist.getTypename());
                if (vehiclelist.getBgimage() != null && vehiclelist.getBgimage().length() > 0){
                    String imgUrl = vehiclelist.getBgimage();
                    queue.getBigBitmap(queue,imgUrl,vehicleViewHolder.imageView);
                }
            }
        }


        return convertView;
    }
    class VehicleViewHolder{
        TextView titleTv,timeTv,typenameTv,reviewcountTv;
        ImageView imageView;
        public VehicleViewHolder(View view){
            titleTv = (TextView) view.findViewById(R.id.titleTv);
            timeTv = (TextView) view.findViewById(R.id.timeTv);
            typenameTv = (TextView) view.findViewById(R.id.typenameTv);
            reviewcountTv = (TextView) view.findViewById(R.id.reviewcountTv);
            imageView = (ImageView) view.findViewById(R.id.imageView);
        }
    }
}
